package com.tianpengtech.front.mobile.task;

import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.task.Task;
import com.tianpengtech.common.model.task.TaskWh;
import com.tianpengtech.common.search.EqualCondition;
import com.tianpengtech.common.search.QueryConstructor;
import com.tianpengtech.front.mobile.common.util.R;
import com.tianpengtech.front.web.task.TaskService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ggd on 2017/7/28 0028.
 */
public class MobileTaskService {
    public static MobileTaskService me = new MobileTaskService();
    public static TaskService taskService = new TaskService();
    private static Task taskDao = Task.dao;
    private static Fair fairDao = Fair.dao;

    public static String PARAM_TASK_NAME="taskName";
    public static String PARAM_TASK_STATUS="taskStatus";

    private static String PARAM_TASK_STATUS_AFTER="1";  //进行中
    private static String PARAM_TASK_STATUS_END="2";    //已完成

    /**
     * 待接任务列表
     * @return
     */
    public R taskIndex(int pageNum, int length,long uid){
        String s=String.format("select %s,%s,%s,%s,%s,%s,%s ",Task.ID,Task.TASK_TITLE,Task.TASK_START_TIME,Task.TASK_END_TIME,Task.TASK_DESCRIPTION,Task.TASK_PRICE,Task.WH_NUMBER);
        String sql=String.format(" from %s task where (task.%s=? and task.%s>?) and task.%s=? and task.%s=? and task.%s not in(select %s from %s tw where tw.%s=?)",
                taskDao.tableName(),Task.STATUS,Task.TASK_START_TIME,Task.DELETETAG,Task.EXTENSION_TYPE,Task.ID,TaskWh.TASK_ID,TaskWh.dao.tableName(),TaskWh.WH_ID);
        Page<Task> page=taskDao.paginate(pageNum,length,s,sql,new Object[]{Task.STATUS_SUCCESS,new Date(),Task.DELETE_STATUS_NO,Task.EXT_TYPE_RL,uid});
        page.getList().forEach( t ->{
            t.put("rece",t.getRece());
            t.put("dateString",t.getDateString());
        });
        R r = R.ok();
        r.p("task",page);
        return r;
    }






    /**
     * 任务列表分页
     * @param
     * @return
     */
    public R findTask(int pageNum, int length,Map<String,Object> params){
        List<Object> param=new ArrayList<>();
        String s = "select task.task_title,task.task_description,task.task_price,task.status as status,task.id as id,task.task_start_time,task.task_end_time ";
        StringBuffer sql= new StringBuffer(" from " + taskDao.tableName() + " task left join " + TaskWh.dao.tableName() + " wh on task." + Task.ID + "=wh." + TaskWh.TASK_ID +" left join "+ Fair.dao.tableName()+" fair on fair."+Fair.ID+"=wh."+TaskWh.WH_ID);
        sql.append(" where task." + Task.DELETETAG+"=? and wh."+TaskWh.WH_ID+"=? and task."+Task.STATUS+"!="+Task.STATUS_WAIT );
        param.add(Task.DELETE_STATUS_NO);
        param.add(params.get("user"));
        if (params!=null) {
            Object taskName = params.get(PARAM_TASK_NAME);
            Object taskStatus = params.get(PARAM_TASK_STATUS);
            if (taskName!=null && StrKit.notBlank(taskName.toString())) {
                sql.append(" and (");
                sql.append("task." + Task.TASK_TITLE+" like ? or ");
                param.add("%" + taskName.toString() + "%");
                sql.append("fair." + Fair.NICK_NAME+" like ?)");
                param.add("%" + taskName.toString() + "%");
            }
            if(taskStatus!=null && StrKit.notBlank(taskStatus.toString())){
                sql.append(" and (");
                if(taskStatus.toString().equals(PARAM_TASK_STATUS_AFTER)){
                    sql.append("task." + Task.STATUS+"=? and task."+Task.TASK_START_TIME+"<?)");
                    param.add(Task.STATUS_SUCCESS);
                    param.add(new Date());
                }else if(taskStatus.toString().equals(PARAM_TASK_STATUS_END)){
                    sql.append("task." + Task.STATUS+"=?)");
                    param.add(Task.STATUS_COMPLETED);
                }
            }
            sql.append(" group by task."+Task.ID+" order by task."+Task.ID+" DESC");
        }
        Page<Task> page=taskDao.paginate(pageNum,length,s,sql.toString(),param.toArray());
        List<Task> tasks = page.getList();
        for (Task t:tasks) {
            t.put("dateString",t.getDateString());
        }
        R r = R.ok();
        r.p("count",page.getTotalRow());
        r.p("pageNum",page.getPageNumber());
        r.p("list",page.getList());
        return r;
    }

    public R getTask(long id,long uid) {
        StringBuffer sql = new StringBuffer("select task.task_title,task.task_description,task.task_price,task.status as status,task.id as id,task.task_start_time,task.task_end_time,wh.click_num,wh.cvs_num,wh.id as whid,task.task_url from " +
                taskDao.tableName() + " task left join " + TaskWh.dao.tableName() + " wh on task." + Task.ID + "=wh." + TaskWh.TASK_ID + " where wh." + TaskWh.TASK_ID + "=? and wh." + TaskWh.WH_ID + "=?");
        Task task = taskDao.findFirst(sql.toString(), id, uid);
        if(task==null){
            sql = new StringBuffer("select task.task_title,task.task_description,task.task_price,task.status as status,task.id as id,task.task_start_time,task.task_end_time,wh.click_num,wh.cvs_num,wh.id as whid,task.task_url from " +
                    taskDao.tableName() + " task left join " + TaskWh.dao.tableName() + " wh on task." + Task.ID + "=wh." + TaskWh.TASK_ID + " where task." + Task.ID + "=?");
            task = taskDao.findFirst(sql.toString(), id);
        }else{
            task.put("sumClick", task.getClick());
            task.put("sumCvs", task.getCvs());
        }
        task.put("dateString", task.getDateString());
        R r = R.ok();
        r.p("task", task);
        if (task.getInt("status")!=Task.STATUS_SUCCESS) {
            String fairs = String.format("select wh.photo,wh.nick_name,wh.id as id,task.task_price,wt.cvs_num,wt.click_num from %s wt left join %s wh on wt.%s=wh.%s left join %s task on wt.%s=task.%s where wt.%s=? order by wt.%s desc", TaskWh.dao.tableName(), Fair.dao.tableName(), TaskWh.WH_ID, Fair.ID, Task.dao.tableName(), TaskWh.TASK_ID, Task.ID, TaskWh.TASK_ID, TaskWh.CVS_NUM);
            List<Fair> fairses = fairDao.find(fairs, id);
            for (int i = 0; i < fairses.size(); i++) {
                Fair f = fairses.get(i);
                f.put("no", i + 1);
            }
            r.p("fairs", fairses);
        }
        return r;
    }

    /**
     * 接受任务
     * @param taskId
     * @param uid
     * @return
     */
    public R acceptTask(String taskId,long uid){
        Ret ret=taskService.saveTaskWait(new String[]{uid+""},taskId);
        if (ret.isOk()){
            return R.ok();
        }else{
            return R.fail(ret.getStr("msg"));
        }
    }
}
